//
//  VisitorLoginView.swift
//  SinaWeiBo20
//
//  Created by tarena on 16/3/18.
//  Copyright © 2016年 tarena. All rights reserved.
//

import UIKit
/** 
 访客视图
 */

//按钮的点击事件代理
protocol VisitorLoginViewDelegate: NSObjectProtocol {
    //登录
    func visitorWillLogin()
    
    //注册
    func visitorWillRegister()
}

class VisitorLoginView: UIView {
    
    //声明代理属性
    //属性默认是强引用  添加weak 弱引用
   weak var visitorDelegate: VisitorLoginViewDelegate?
    
    @objc func loginBtnDidClick() {
        visitorDelegate?.visitorWillLogin()
    }
    @objc func registorBtnDidClick() {
        visitorDelegate?.visitorWillRegister()
    }
    
    func setUIInfo(imageName: String?, title: String){
        circleView.hidden = false
        if imageName != nil {
            iconView.image = UIImage(named: imageName!)
            circleView.hidden = true
        }else{
            //首页动画
            startAnimation()
        }
        tipLabel.text = title
    }
    
    /** 添加旋转动画*/
    private func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT
        anim.duration = 15
        anim.toValue = 2 * M_PI
        
        //动画结束 或 视图处于不活跃的状态的时  不会被移除
        anim.removedOnCompletion = false
        
        circleView.layer.addAnimation(anim, forKey: nil)
    }
    
    
    init() {
        super.init(frame: CGRectZero)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /** 设置访客视图*/
    private func setupUI() {
        backgroundColor = UIColor(white: 0.93, alpha: 1.0)
        addSubview(circleView)
        addSubview(backView)
        addSubview(iconView)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        
        //设置frame 布局失效
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //设置布局
        layoutConstraint()
        
        //添加点击事件
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registorBtnDidClick", forControlEvents: .TouchUpInside)
    }
    
    /** 子视图布局*/
    private func layoutConstraint(){
        
        /** CircleView 的约束*/
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        
        /** iconView 的约束*/
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        
        /** tipLabel 的约束*/
         addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 15))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 250))
        
        /** 登录按钮的约束 */
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 15))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil , attribute: .NotAnAttribute, multiplier: 1.0, constant: 90))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil , attribute: .NotAnAttribute, multiplier: 1.0, constant: 44))
        
        
        /** 注册按钮的约束*/
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 15))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil , attribute: .NotAnAttribute, multiplier: 1.0, constant: 90))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil , attribute: .NotAnAttribute, multiplier: 1.0, constant: 44))
        
        /** 背景视图的约束*/
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView":backView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-40)-[button]", options: [], metrics: nil , views: ["backView":backView, "button":registerBtn]))
        
        
    }
    
    //MARK : 懒加载所有的控件
    
    //圆形环绕图标
    private lazy var circleView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //house 图标
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //提示标签
    private lazy var tipLabel: UILabel = {
        let la = UILabel()
        la.text = "关注一些人，回这里看看有什么惊喜"
        la.textAlignment = NSTextAlignment.Center
        la.font = UIFont.systemFontOfSize(14)
        la.textColor = UIColor.lightGrayColor()
        la.numberOfLines = 0
        return la
    }()
    
    //登录按钮
    private lazy var loginBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return btn
    }()
    
    //注册按钮
    private lazy var registerBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        return btn
    }()
    
    //背景视图
    private lazy var backView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
}
